
if( CharacterMenu ) then

	if( CharacterMenu.TitleBar ) then

		CharacterMenu.TitleBar:Remove();
		CharacterMenu.TitleBar = nil;
		
	end
		
	CharacterMenu:Remove();
	CharacterMenu = nil;

end

local ChosenCharacterModel = "";

local FreeStatPoints = 50;
local MaxFreeStats = FreeStatPoints;

function CreateCharacterMenu()
	
	CharacterMenu = CreateBPanel( "Character Create", ScrW() / 2 - 300, math.Clamp( ScrH() / 2 - 210, ScrH() * .1 + 70, 9999 ), 600, 380 );
	CharacterMenu:CanClose( true );
	CharacterMenu:CanDrag( false );
	CharacterMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	CharacterMenu.OnClose = function()
	
		RunConsoleCommand( "rp_closecharcreate", "" );
	
	end
	
	local x, y = CharacterMenu:GetPos();
	
		CharacterMenu.PaintHook = function()
	
		local strperc = ClientVars["Strength"] / 100;
		local spdperc = ClientVars["Speed"] / 100;
		local endperc = ClientVars["Endurance"] / 100;
		local aimperc = ClientVars["Aim"] / 100;
		local freeperc = FreeStatPoints / MaxFreeStats;
	
		draw.DrawText( "Strength - " .. ClientVars["Strength"], "SmallChatFont", 260, 135, Color( 255, 255, 255, 255 ) );
		draw.RoundedBox( 2, 255, 150, 100, 20, Color( 0, 0, 0, 255 ) );
		draw.RoundedBox( 2, 256, 151, 98 * strperc, 18, Color( 0, 128, 0, 255 ) );
		draw.RoundedBox( 2, 256, 151, 98 * strperc, 6, Color( 255, 255, 255, 20 ) );
		draw.DrawText( "Speed - " .. ClientVars["Speed"], "SmallChatFont", 260, 175, Color( 255, 255, 255, 255 ) );
		draw.RoundedBox( 2, 255, 190, 100, 20, Color( 0, 0, 0, 255 ) );
		draw.RoundedBox( 2, 256, 191, 98 * spdperc, 18, Color( 0, 128, 0, 255 ) );
		draw.RoundedBox( 2, 256, 191, 98 * spdperc, 6, Color( 255, 255, 255, 20 ) );
		draw.DrawText( "Endurance - " .. ClientVars["Endurance"], "SmallChatFont", 260, 215, Color( 255, 255, 255, 255 ) );
		draw.RoundedBox( 2, 255, 230, 100, 20, Color( 0, 0, 0, 255 ) );
		draw.RoundedBox( 2, 256, 231, 98 * endperc, 18, Color( 0, 128, 0, 255 ) );
		draw.RoundedBox( 2, 256, 231, 98 * endperc, 6, Color( 255, 255, 255, 20 ) );
		draw.DrawText( "Aim - " .. ClientVars["Aim"], "SmallChatFont", 260, 255, Color( 255, 255, 255, 255 ) );
		draw.RoundedBox( 2, 255, 270, 100, 20, Color( 0, 0, 0, 255 ) );
		draw.RoundedBox( 2, 256, 271, 98 * aimperc, 18, Color( 0, 128, 0, 255 ) );
		draw.RoundedBox( 2, 256, 271, 98 * aimperc, 6, Color( 255, 255, 255, 20 ) );
		draw.DrawText( "Free Stat Points - " .. FreeStatPoints, "SmallChatFont", 260, 295, Color( 255, 255, 255, 255 ) );
		draw.RoundedBox( 2, 255, 310, 100, 20, Color( 0, 0, 0, 255 ) );
		
		if( freeperc > 0 ) then
		
			draw.RoundedBox( 2, 256, 311, 98 * freeperc, 18, Color( 0, 0, 128, 255 ) );
			draw.RoundedBox( 2, 256, 311, 98 * freeperc, 6, Color( 255, 255, 255, 20 ) );

		end
		
		draw.DrawText( "There is a bug when creating a character ingame, rejoin the server to make one!", "ChatFontSub", 0, 0, Color( 255, 0, 0, 255 ) )

	end
	
	--Exploit this and see what happens
	local function Add( stat )
	
		if( FreeStatPoints > 0 ) then
	
			ClientVars[stat] = ClientVars[stat] + 1;
			FreeStatPoints = FreeStatPoints - 1;
			
		end
	
	end
	
	local function Subtract( stat )
	
		if( ClientVars[stat] > 10 ) then
	
			ClientVars[stat] = ClientVars[stat] - 1;
			FreeStatPoints = FreeStatPoints + 1;
			
		end		
	
	end
	
	CharacterMenu:AddButton( "-", 230, 150, function() Subtract( "Strength" ) end );
	CharacterMenu:AddButton( "-", 230, 190, function() Subtract( "Speed" ) end );
	CharacterMenu:AddButton( "-", 230, 230, function() Subtract( "Endurance" ) end );
	CharacterMenu:AddButton( "-", 230, 270, function() Subtract( "Aim" ) end );
		
	CharacterMenu:AddButton( "+", 364, 150, function() Add( "Strength" ) end );
	CharacterMenu:AddButton( "+", 364, 190, function() Add( "Speed" ) end );
	CharacterMenu:AddButton( "+", 364, 230, function() Add( "Endurance" ) end );
	CharacterMenu:AddButton( "+", 364, 270, function() Add( "Aim" ) end );
	
	CharacterMenu:AddLabel( "You must give both a first\nand second name and it\nmust be between 7 and 38\ncharacters.", "ChatFontSub", 245, 45, Color( 255, 255, 255, 255 ) );
	CharacterMenu:AddLabel( "Full Name", "TargetID", 8, 8, Color( 255, 255, 255, 255 ) );
	CharacterMenu:AddLabel( "Age", "TargetID", 8, 28, Color( 255, 255, 255, 255 ) );
	CharacterMenu:AddLabel( "Model", "TargetID", 8, 48, Color( 255, 255, 255, 255 ) );
	
	CharacterMenu.NameEntry = vgui.Create( "DTextEntry", CharacterMenu );
	CharacterMenu.NameEntry:SetPos( x + 90, y + 11 );
	CharacterMenu.NameEntry:SetSize( 200, 15 );
	CharacterMenu.NameEntry:MakePopup();
	
	ApplyMaxCharacters( CharacterMenu.NameEntry, 38 );
	
	CharacterMenu.AgeEntry = vgui.Create( "DTextEntry", CharacterMenu );
	CharacterMenu.AgeEntry:SetPos( x + 90, y + 32 );
	CharacterMenu.AgeEntry:SetSize( 20, 15 );
	CharacterMenu.AgeEntry:MakePopup();
	
	ApplyMaxCharacters( CharacterMenu.AgeEntry, 2 );
	
	CharacterMenu.ModelSelectPanel = CreateBPanel( nil, 5, 63, 216, 256 );
	CharacterMenu.ModelSelectPanel:SetParent( CharacterMenu );
	CharacterMenu.ModelSelectPanel:SetBodyColor( Color( 30, 30, 30, 255 ) );
	CharacterMenu.ModelSelectPanel:CanClose( false );
	CharacterMenu.ModelSelectPanel:CanDrag( false );
	CharacterMenu.ModelSelectPanel:CanSeeTitle( false );
	CharacterMenu.ModelSelectPanel:EnableScrolling( true );
	
	CharacterMenu.ModelViewPanel = CreateBPanel( nil, 400, 0, 190, 350 );
	CharacterMenu.ModelViewPanel:SetParent( CharacterMenu );
	CharacterMenu.ModelViewPanel:CanClose( false );
	CharacterMenu.ModelViewPanel:CanDrag( false );
	CharacterMenu.ModelViewPanel:CanSeeTitle( false );
	CharacterMenu.ModelViewPanel:EnableScrolling( true );
	CharacterMenu.ModelViewPanel:SetBodyColor( Color( 30, 30, 30, 255 ) );
	
	
	local function SetPlayerViewModel( model )
	
		ChosenCharacterModel = model;
	
		if( not CharacterMenu.ModelViewPanel.Model ) then
		
			CharacterMenu.ModelViewPanel.Model = vgui.Create( "DModelPanel", CharacterMenu.ModelViewPanel );
			CharacterMenu.ModelViewPanel.Model:SetPos( -70, 0 );
			CharacterMenu.ModelViewPanel.Model:SetSize( 350, 350 );
			CharacterMenu.ModelViewPanel.Model:SetModel( model );
			
			CharacterMenu.ModelViewPanel.Model.LayoutEntity = function( self )
			    
			    self:RunAnimation();
			    
		 	end
		 	
			
			CharacterMenu.ModelViewPanel.Model:SetFOV( 100 );
			CharacterMenu.ModelViewPanel.Model:SetLookAt( Vector( 0, 0, 50 ) );
	 		CharacterMenu.ModelViewPanel.Model:SetCamPos( Vector( 30, -10, 55 ) );

		
		else
		
			CharacterMenu.ModelViewPanel.Model:SetModel( model );
		
		end
	
	end
	
	CharacterMenu.ModelSelectPanel.Models = { }
	local modelx = 0;
	local modely = 0;
	
	for k, v in pairs( TS.SelectablePlayerModels ) do
	
		CharacterMenu.ModelSelectPanel.Models[k] = vgui.Create( "SpawnIcon", CharacterMenu.ModelSelectPanel );
		CharacterMenu.ModelSelectPanel.Models[k]:SetModel( v );
		CharacterMenu.ModelSelectPanel.Models[k]:SetPos( modelx, modely );
		CharacterMenu.ModelSelectPanel.Models[k]:SetSize( 64, 64 );
		CharacterMenu.ModelSelectPanel.Models[k].LayoutEntity = function()
		    
	 	end
	 	
	 	CharacterMenu.ModelSelectPanel.Models[k].DoClick = function()
	 	
	 		SetPlayerViewModel( v );
	 	
	 	end
	 	
	 	CharacterMenu.ModelSelectPanel:AddObject( CharacterMenu.ModelSelectPanel.Models[k] );
	 	
	 	modelx = modelx + 64;
	 	
	 	if( modelx >= 192 ) then
	 	
	 		modely = modely + 64;
	 		modelx = 0;
	 	
	 	end
	 	
	 end
	 
	CharacterMenu.CreateCharacterButton = CharacterMenu:AddButton( "Create Character", 10, 345 );
	
	CharacterMenu.CreateCharacterButton.Action = function()
	
		if( string.len( CharacterMenu.NameEntry:GetValue() ) < 7 or
			string.len( CharacterMenu.NameEntry:GetValue() ) > 38 ) then
		
			CreateOkPanel( "Invalid name.", "Your name needs to be between 7 and 38 characters." );
			return;		
		
		end
	
		if( string.gsub( CharacterMenu.NameEntry:GetValue(), " ", "" ) == "" ) then
		
			CreateOkPanel( "Invalid name.", "Your name is blank, queer." );
			return;		
		
		end
	
		if( not tonumber( CharacterMenu.AgeEntry:GetValue() ) ) then
		
			CreateOkPanel( "Invalid number", "Invalid age." );
			return;
		
		end
		
		if( tonumber( CharacterMenu.AgeEntry:GetValue() ) < 18 or
			tonumber( CharacterMenu.AgeEntry:GetValue() ) > 80 ) then
			
			CreateOkPanel( "Invalid number", "Age must be between 18 and 80." );
			return;			
			
		end
	
		if( ChosenCharacterModel == "" ) then
		
			CreateOkPanel( "Invalid model", "Choose a model." );
			return;
		
		end
		
		if( not table.HasValue( TS.SelectablePlayerModels, ChosenCharacterModel ) ) then
		
			CreateOkPanel( "Invalid model", "HOW'D YOU GET THAT MODEL YOU FUCKING FAGGOT" );
			return;			
		
		end
		
		RunConsoleCommand( "eng_ccsetname", CharacterMenu.NameEntry:GetValue() );
		RunConsoleCommand( "eng_ccsetage", CharacterMenu.AgeEntry:GetValue() );
		RunConsoleCommand( "eng_ccsetmodel", ChosenCharacterModel );
		RunConsoleCommand( "eng_ccsetstats", ClientVars["Strength"], ClientVars["Endurance"], ClientVars["Speed"], ClientVars["Aim"] );
		RunConsoleCommand( "eng_createcharacter", "" );
	
	end
	
	print( LocalPlayer():Nick() .. ": Character menu prompted.")
	
end
usermessage.Hook( "PCM", CreateCharacterMenu );

if( ChooseMenu ) then

	if( ChooseMenu.TitleBar ) then

		ChooseMenu.TitleBar:Remove();
		ChooseMenu.TitleBar = nil;
		
	end

	ChooseMenu:Remove();
	ChooseMenu = nil;

end

function CreateChooseMenu( msg )
	
	RunConsoleCommand( "eng_getchars", UniqueID );
	CharacterSaves = { };
	
	if( IntroFade > 0 ) then
		TS.WaitingToMakeCharacterMenu = true;
		return;
	end
	
	local canclose;
	
	if( type( msg ) == "boolean" ) then
		canclose = msg;
	else
		canclose = msg:ReadBool();
	end
	
	local title = nil;
	
	if( canclose ) then
		title = "Character Select";
	end

	ChooseMenu = CreateBPanel( canclose, ScrW() / 2 - 150, math.Clamp( ScrH() / 2 - 210, ScrH() * .1 + 70, 9999 ), 180, 300 );
	ChooseMenu:CanClose( false );
	ChooseMenu:CanDrag( false );
	ChooseMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	ChooseMenu.Frame = CreateBPanel( nil, 8, 5, 162, 250 )
	ChooseMenu.Frame:SetParent( ChooseMenu )
	ChooseMenu.Frame:SetBodyColor( Color( 30, 30, 30, 255 ) );
	ChooseMenu.Frame:EnableScrolling( true )
	
	local y = 0
	
	ChooseMenu.Frame.PaintHook = function()
	
		for k, v in pairs( CharacterSaves ) do
				
			spawnicon = vgui.Create( "SpawnIcon", ChooseMenu );
			spawnicon:SetModel( v.Model );
			spawnicon:SetIconSize( 32, 32 ) 
			spawnicon:SetPos( 15, y + 30 )
			spawnicon:SetToolTip( v.Name );
			
			local nick = v.Name;
			
			if( string.len( nick ) > 10 ) then
				nick = string.sub( nick, 1, 10 ) .. "..";
			end
		
			ChooseMenu.SelectBut = ChooseMenu:AddButton( nick, 50, y + 33 )
			ChooseMenu.SelectBut.Action = function()
		
				RunConsoleCommand( "eng_selectchar", UniqueID, v.ID );
				RunConsoleCommand( "rp_closecharcreate" )
			
			end
			
			CharacterSaves = { };
			
			--CharMenu.DeleteButton = ChooseMenu:AddButton( "Delete", 65, y + 33 )
			--CharMenu.DeleteButton.Action = function() end
			
			y = y + 34
			
			ChooseMenu.Frame:AddObject( spawnicon )
			
		end
		
	end	

	ChooseMenu.CreateBut = ChooseMenu:AddButton( "Create new character", 13, 275 );
	ChooseMenu.CreateBut.Action = function()
		CreateCharacterMenu();
		if( ChooseMenu ) then

			if( ChooseMenu.TitleBar ) then
				ChooseMenu.TitleBar:Remove();
				ChooseMenu.TitleBar = nil;	
			end
			ChooseMenu:Remove();
			ChooseMenu = nil;
		end
	end	

	gui.EnableScreenClicker( true );
	
end	
--usermessage.Hook( "CCM", CreateChooseMenu )

function msgRemoveCharacterChooseMenu()

	if( ChooseMenu ) then

		if( ChooseMenu.TitleBar ) then

			ChooseMenu.TitleBar:Remove();
			ChooseMenu.TitleBar = nil;
			
		end

		ChooseMenu:Remove();
		ChooseMenu = nil;
		
	end
	
end
usermessage.Hook( "RCCM", msgRemoveCharacterChooseMenu )	

function RemoveCharacterMenu()
	
	if( CharacterMenu and CharacterMenu.TitleBar ) then
	
		CharacterMenu.TitleBar:Remove();
		CharacterMenu:Remove();

	end

end
usermessage.Hook( "RCM", RemoveCharacterMenu );